class IssueController < ApplicationController
  
  def index
  end

#  def index
#    session[:active_project] = nil
#    list
#   render :action => 'list'
#  end

  def update_list
    customlist
    render :action => 'list'
  end
  
  def complete
    totallist
    render :action => 'list'
  end
  
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }
       

  def list
    @issue_pages, @issues = paginate :issues, :per_page => 10, :conditions => "complete = 'false'"
  end

  def totallist
    @issue_pages, @issues = paginate :issues, :per_page => 10, :conditions => ["project_key = ?", session[:active_project]]
  end

  def customlist
    if params[:static_project] then
      session[:active_project] = params[:static_project]
    end
    @issue_pages, @issues = paginate :issues, :per_page => 10, :conditions => ["complete = 'false' and project_key = ?", session[:active_project]]
  end
  
  def active_project
    redirect_to(:action => "list")    
  end
  
  def show
    @issue = Issue.find(params[:id])
  end

  def new
    @issue = Issue.new
  end

  def create
    @issue = Issue.new(params[:issue])
    if @issue.save
      flash[:notice] = 'Issue was successfully created.'
      list
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @issue = Issue.find(params[:id])
  end

  def update
    @issue = Issue.find(params[:id])
    if @issue.update_attributes(params[:issue])
      flash[:notice] = 'Issue was successfully updated.'
      redirect_to :action => 'show', :id => @issue
    else
      render :action => 'edit'
    end
  end

  def destroy
    Issue.find(params[:id]).destroy
    list
    redirect_to :action => 'list'
  end
  
  def comment
    @tmp_comment = Issue.find(params[:id]).issue_comments.create(params[:comment])
    @tmp_comment.update_attributes(:create_date => Time.now)
    flash[:notice] = "Added your comment."
    redirect_to :action => "show", :id => params[:id]
  end
end
